/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.swing.dialog;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
 *
 * @author Administrator
 */
public abstract class AbstractKeyBoardJDialog  extends JDialog {
   private ActionListener actionListener;

   /**
    * @wbp.parser.constructor
    */
   protected AbstractKeyBoardJDialog(Frame parent, boolean modal) {
        super(parent, modal);
    }

   protected AbstractKeyBoardJDialog(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
    }  

    protected JRootPane createRootPane() {

        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        JRootPane rootPane = new JRootPane();
        actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                escapeKeyProc();
            }
        };
        rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                enterKeyProc();
            }
        };
        rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

        stroke = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
        actionListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                upKeyProc();
            }
        };

  rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  stroke = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
  actionListener = new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent actionEvent) {
     downKeyProc();
    }
  };

  rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
  return rootPane;
}

    protected static Window getWindow(Component parent) {
        if (parent == null) {
            return new JFrame();
        } else if (parent instanceof Frame || parent instanceof Dialog) {
            return (Window)parent;
        } else {
            return getWindow(parent.getParent());
        }
    }    
    
    
  protected abstract void enterKeyProc();
  protected abstract void escapeKeyProc();
  protected abstract void upKeyProc();
  protected abstract void downKeyProc();

}


